// pages/publishvideo/publishvideo.js
Page({
  data: {
    tags: [],
    src:"",
    showControls:false,
    nickName: '车神大咖'
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.request({
      url: 'https://api.jiefu.tv/app2/api/videoTag/list.html',
      data: {
        "wxAccount": "30",
        "pageSize": 1000
      },
      success: function(res){
        that.setData({
          tags: res.data.data
        })
      },
      fail: function(res){

      }
    })
    wx.getUserInfo({
      success: function (res) {
        var userInfo = res.userInfo
        that.setData({
          nickName: userInfo.nickName
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  onClickChoose: function(event){
    var that = this
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 20,
      success: function (res) {
        that.setData({
          "src": res.tempFilePath,
          "duration": res.duration,
          "size": res.size,
          "height": res.height,
          "width:": res.width
        })
        console.log(res)   
        console.log(res.size)
      }
    })
  },
  onItemClick: function(event){
    var pos = event.currentTarget.dataset.position
    var tempArray = this.data.tags
    var temp = tempArray[pos]
    temp.choose = !temp.choose
    this.setData({
      tags: tempArray
    })
  },
  onClickPublish: function(event){
    console.log("tap")
    if(this.data.src==""){
      wx.showToast({
        title: '请先选择或者拍摄视频'
      })
      return
    }
    var tag = ""
    for(let i = 0;i<this.data.tags.length;i++){
      if(this.data.tags[i].choose){
        if (tag!=""){
          tag = tag + ',' + this.data.tags[i].name
        }else {
          tag = tag + this.data.tags[i].name
        }
      }
    }
    var that = this
        wx.showToast({
          title: '正在上传中',
          icon: 'loading',
          duration: 60000
        })
        wx.uploadFile({
          url: 'https://api.jiefu.tv/app2/api/videoArticle/upload.html',
          filePath: that.data.src,
          name: 'uploadFile',
          formData: {
            "wxAccount": "30",
            'title': that.data.nickName,
            'intro': '',
            'type': '1',
            'tagNames': tag,
            'userId': wx.getStorageSync("userId"),
            'anony': '0'
          },
          success: function (res) {
            var data = res.data
            console.log("upload-->"+data)
            wx.hideToast()
            var mydata = JSON.parse(res.data)
            if (mydata.code=="918"){
              wx.showToast({
                title: '该视频已经上传过,请不要重复上传',
                duration: 2000
              })
            } else if(mydata.code=="0"){
              wx.showToast({
                title: '上传成功',
                icon: 'success',
                duration: 2000,
                success: function () {
                  wx.navigateTo({
                    url: '../publish/publish',
                  })
                }
              })
            }else{
              wx.showToast({
                title: '视频大小不能超过40M',
                duration: 2000
              })
            }
          },
          fail: function () {
            wx.showToast({
              title: '上传失败',
              icon: 'fail',
              duration: 2000
            })
          },
          complete: function () {
          }
        })

  }
})